//
//  HomeViewController.m
//  lv678
//
//  Created by apple on 15/6/11.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//

#import "HomeViewController.h"
#import "TicketSortCollection.h"
#import "AppDelegate.h"
#import "loginSkip.h"
#import "TravelEnorllController.h"
#import "Reachability.h"
@interface HomeViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;//滚动视图
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;//pageControl
@property (nonatomic,strong)NSTimer *time;//定时器
@property (weak, nonatomic) IBOutlet UIButton *travelEnroll;//旅游报名button

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
};

#pragma mark - 初始化tableview
- (void)initTableView{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.pageControl.numberOfPages = 3;
    _scrollView.delegate = self;
    //获取scrollview的坐标
    CGFloat imageW = self.view.frame.size.width;
    CGFloat imageH = self.scrollView.frame.size.height;
    CGFloat imageY = self.scrollView.frame.origin.y;
    //动态创建uiimageview添加到uiscrollview中
    for (int i =0; i<3; i++) {
        //初始化imageview
        UIImageView  *imageView = [[UIImageView alloc]init];
        //设置imageName
        NSString *imageName     = [NSString stringWithFormat:@"jingdian%02d",i+1];
        imageView.image         = [UIImage imageNamed:imageName];
        //获取横向x轴坐标
        CGFloat imageX          = imageW*i;
        imageView.frame         = CGRectMake(imageX, imageY, imageW, imageH);
        //添加到scrollview视图中
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize                    = CGSizeMake(self.view.frame.size.width * 3, 0);
    self.scrollView.pagingEnabled                  = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    //创建计时器控件 NSTimer
    _time = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    //获取当前的消息循环对象
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:_time forMode:NSRunLoopCommonModes];
    
    //跳转视图
    [self.travelEnroll addTarget:self action:@selector(pushTravelEnroll) forControlEvents:UIControlEventTouchUpInside];
}

#pragma  mark - 判断是否登陆，如果登陆则跳转旅游报名视图，反之登陆页
- (void)pushTravelEnroll{
    AppDelegate *app = [[AppDelegate alloc]init];
    TravelEnorllController *travelEnroll = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"travelEnroll"];
    if ([app.loginInfo objectForKey:@"userName"] == nil) {
        UIViewController *login = [loginSkip skipEnrollLoginView:travelEnroll];
        [self.navigationController pushViewController:login animated:YES];
    }else{
        [self presentViewController:travelEnroll animated:YES completion:nil];

    }
}

#pragma mark - 设置滚动图片
-(void)scrollImage{
    //设置滚动一次图片
    NSInteger page = self.pageControl.currentPage;
    if (page == self.pageControl.numberOfPages -1) {
        page = 0;
    }else{
        page++;
    }
    
    CGFloat offsetX = page * self.scrollView.frame.size.width;
    
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
}
//开始拖拽，停止定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //停止计时器，计时器一旦停止就无法重新启用，下次必须创建一个新的定时器
    [_time invalidate];
    
    _time = nil;
}
#pragma mark - 停止拖拽，重新创建一个定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    _time               = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    NSRunLoop * runLoop = [NSRunLoop currentRunLoop];
        [runLoop addTimer:_time forMode:NSRunLoopCommonModes];
}

#pragma mark - 计算坐标
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //获取图片偏移坐标
    CGFloat offsetX          = _scrollView.contentOffset.x;

    offsetX                  = offsetX + (self.scrollView.frame.size.width) * 0.5;
    //计算当前图片索引
    int page                 = offsetX/scrollView.frame.size.width;

    _pageControl.currentPage = page;
}

#pragma mark - 页面动画出现时隐藏bar
- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
}

#pragma mark - 判断是否联网
/***
 * 此函数用来判断是否网络连接服务器正常
 * 需要导入Reachability类
 */
- (BOOL)isExistenceNetwork
{
    BOOL isExistenceNetwork;
    Reachability *reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];  // 测试服务器状态
    
    switch([reachability currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = FALSE;
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = TRUE;
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = TRUE;
            break;
    }
    return  isExistenceNetwork;
}

@end
